#!/bin/sh
# chkconfig: 345 98 20
# description: webnamed server
# processname: webnamed

# description for chkconfig: 345 98 20
#  345: run levels
#  98: priority to start
#  20: priority to stop

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/webnamed
NAME=webnamed
DESC="webnamed server"
PIDFILE=/var/run/$NAME.pid
export LANG=C
test -x $DAEMON || exit 0

if [ -f /etc/default/$NAME ]; then
    . /etc/default/$NAME
fi

DAEMON_OPTS=""

start_proc() {
    pid=`pidof $DAEMON`
    if [ "$pid" != "" ]; then
        echo $NAME is already running.
        return 1
    fi
    $DAEMON $DAEMON_OPTS
    return "$?"
}

stop_proc() {
    pid=`pidof $DAEMON`
    if [ "$pid" != "" ]; then
        kill $pid
        [ "$?" != "0" ] && return 1
        sleep 1
        pid=`pidof $DAEMON`
        if [ "$pid" != "" ]; then
            kill -9 $pid
            [ "$?" != "0" ] && return 1
        fi
    fi
    rm -f $PIDFILE
    return 0
}

restart_proc() {
    stop_proc
    [ "$?" != "0" ] && return 1
    start_proc
    return "$?"
}

status_proc() {
    kill -0 `pidof $DAEMON` 2>/dev/null
    return "$?"
}

case "$1" in
    start)
        echo "Starting ${DESC}"
        start_proc
        [ "$?" = "0" ] || echo "failed"
        ;;
    stop)
        echo "Shutting down ${DESC}"
        stop_proc
        [ "$?" = "0" ] || echo "failed"
        ;;
    restart|force-reload)
        echo "Restarting ${DESC}"
        restart_proc
        [ "$?" = "0" ] || echo "failed"
        ;;
    status)
        status_proc
        [ "$?" = "0" ] && echo "$DESC is alive" || echo "$DESC is dead"
        echo
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}" >&2
        exit 1
        ;;
esac
exit 0
